import hashlib
import os


def get_md5(filename):
    md5_obj = hashlib.md5()
    content = read_file(filename)
    md5_obj.update(content)
    md5 = md5_obj.hexdigest()
    return md5


def read_file(filename):
    with open(filename, "rb") as f:
        content = f.read()
    return content


def file_end_seek(filename):
    with open(filename, "rb") as f:
        f.seek(-1)
        return f.tell()


def return_seek_data(filename, seek):
    with open(filename, "rb") as f:
        start_line = seek
        end_line = file_end_seek(filename)
        content = f.readlines()[start_line: end_line + 1]
        return content


def return_data(request):
    # new_md5 = get_md5(request.name)
    # if checkout_md5(request.md5, new_md5):
    #    content = return_seek_data(request.name, request.seek)
    #    return content
    #else:
    #    return b""
    filename = request.name
    content = read_file(filename)
    return content


def checkout_md5(old_md5=None, new_md5=None):
    """需要不需要同步数据"""
    if old_md5 == new_md5:
        return False
    return True


def get_all_file(dirname: str):
    listFiles = []
    for i, j, k in os.walk(dirname):
        if len(k) > 0:
            for _k in k:
                listFiles.append(i + "/" + _k)
    return listFiles
